<?php
#
# build the API URL to call
#

$NOPARAMS	= array();
$secret 	= 'aa5ae7ea5b0ed521';
$api_key 	= '265684b19d4442cac7c96b5c872eb50b';

$rest_url	= 'http://api.flickr.com/services/rest/?';
$auth_url	= 'http://flickr.com/services/auth/?';

global $the_flickr_token;
$auth_token	= $the_flickr_token;

function get_flickr_hash($str)
{	
	global $secret;
	ksort($str);

	$p2 = $secret;
	foreach ($str as $k => $v) {
		$p2 = $p2 . $k . $v;
	}
	return md5($p2);
}

function getFlickrToken($frob) 
{
	global $rest_url;
	$p = array(
		'frob' => $frob,
	);

	$rsp_obj = doFlickrMethod('flickr.auth.getToken', $p, '1', $rest_url);
	return $rsp_obj['auth']['token']['_content'];
}

function doFlickrMethod($method_name, $other_params, $requires_auth, $thisurl)
{
	$encoded_params = array();
	$sort_array = array();

	global $api_key;
	$params = array(
		'api_key'	=> $api_key,
	);

	if (!empty($method_name)) {
		$params['method'] = $method_name;
		$params['format'] = 'php_serial';

	}

	foreach ($params as $k => $v){
		$encoded_params[] = urlencode($k).'='.urlencode($v);
		$sort_array[$k] = $v;
	}

	if (!empty($other_params)) {
		foreach ($other_params as $k => $v){
			$encoded_params[] = urlencode($k).'='.urlencode($v);
			$sort_array[$k] = $v;
		}
	}

	ksort($sort_array);

	if ($requires_auth == '1') {
		$encoded_params[] = 'api_sig=' . get_flickr_hash($sort_array);
	}


	$url = $thisurl . implode('&', $encoded_params);

	$rsp = file_get_contents($url);
	$rsp_obj = unserialize($rsp);

	return $rsp_obj;
}

#
# call the API and decode the response
#

function getFlickrRecentPhotos($how_many)
{
	global $auth_token;
	global $rest_url;
	$p = array(
		'auth_token' 	=> $auth_token,
		'min_date' 	=> 1,
		'per_page'	=> $how_many,		
	);
	$rsp_obj = doFlickrMethod('flickr.photos.recentlyUpdated', $p, '1', $rest_url);
	return $rsp_obj;
}

function getFlickrPhotoInfo($photo_id)
{
	global $rest_url;
	$p = array(
		'photo_id' 	=> $photo_id,
	);
	$rsp_obj = doFlickrMethod('flickr.photos.getInfo', $p, '0', $rest_url);
	return $rsp_obj;
}

function getFlickrPhotoDescription($photo_id)
{
	$rsp_obj = getFlickrPhotoInfo($photo_id);
	return $rsp_obj['photo']['_content']['description'];
}

function getFlickrPhotosFromSet($set_id)
{
	global $rest_url;
	$p = array(
		'photoset_id' 	=> $set_id,
	);
	$rsp_obj = doFlickrMethod('flickr.photoSets.getPhotos', $p, '0', $rest_url);
	return $rsp_obj;	
}

function getFlickrPhoto($photo_id)
{
	global $rest_url;
	$p = array(
		'photoset_id' 	=> $set_id,
	);
	$rsp_obj = doFlickrMethod('flickr.photoSets.getPhotos', $p, '0', $rest_url);
	return $rsp_obj;
}

function getFlickrContactList()
{
	global $auth_token;
	global $rest_url;
	$p = array(
		'auth_token' 	=> $auth_token,
		'page' 	=> '1',
	);
	$rsp_obj = doFlickrMethod('flickr.contacts.getList', $p, '1', $rest_url);
	return $rsp_obj;
}

function getFlickrComments($photo_id)
{
	global $rest_url;
	$p = array(
		'photo_id' 	=> $photo_id,
	);
	$rsp_obj = doFlickrMethod('flickr.photos.comments.getList', $p, '0', $rest_url);
	return $rsp_obj;
}

function getFrob()
{
	global $rest_url;
	$rsp_obj = doFlickrMethod('flickr.auth.getFrob', $NOPARAMS, '1', $rest_url);
	return $rsp_obj['frob']['_content'];
}

function getFlickrThumbnailForContact($nsid)
{
	global $rest_url;
	$p = array(
		'user_id' 	=> $nsid,
	);
	$rsp_obj = doFlickrMethod('flickr.people.getInfo', $p, '0', $rest_url);
	$icon_server = $rsp_obj['person']['iconserver'];
	if ($icon_server == 0) {
		return 'http://www.flickr.com/images/buddyicon.jpg';
	}
	else {
		return 'http://static.flickr.com/' .$icon_server. '/buddyicons/' .$nsid . '.jpg';
	}
}

function getFlickrAuthURL($frob)
{
	global $api_key;
	$p = array(
		'api_key' => $api_key,
		'frob' => $frob,
		'perms' => 'write',
	);

	ksort($p);
	$s = array();
	foreach($p as $a => $b) 
	{
		$s[] = urlencode($a) .'='.urlencode($b);
	}

	$s[] = 'api_sig=' . get_flickr_hash($p);

	$url = 'http://flickr.com/services/auth/?' . implode('&', $s);
	return $url;
}

?>